import i18n from '../i18n/index'
export function getEnv(){
  let env = 'h5'
  if (typeof plus !== 'undefined') {
    env = 'app'
  }
  return env
}
export function imgPath (name,type='src') {
  if(type === 'src'){
    return `/static/images/${name}`
  }else if(String(type).includes('bg')){
    const types = String(type).split('_')
    const imgMode = types[1] || 'cover'
    const imgSize = imgMode==='widthFix'?`100% auto`:imgMode==='heightFix'?`auto 100%`:'100% 100%'
    return `
      background-image: url('/static/images/${name}');
      background-size: ${imgSize};
    `
  }else if(type === 'file'){
    return `/static/${name}`
  }
}
export function convertRem (rem, docWidth) {
  return rem * 1 * (document.documentElement.clientWidth / (docWidth || 750))
}
/**
* 添加控制台
* @param content 提示的标题
*/
export function copyText (text) {
    uni.setClipboardData({
        data: text,
        success: function () {
          uni.showToast({
            title: i18n.t('common.copy')+i18n.t('common.success'),
            icon: 'success'
          });
        },
        fail: function () {
          uni.showToast({
              title: i18n.t('common.copy')+i18n.t('common.fail'),
              icon: 'none'
          });
        }
  });
}
export function showEruda () {
  return new Promise((resolve) => {
    const script = document.createElement('script')
    script.src = './static/js/eruda.min.js'
    script.defer = true
    script.setAttribute('id', 'eruda_Console')
    if (!document.getElementById('eruda_Console')) {
      document.body.appendChild(script)
      script.onload = function () {
        window.eruda.init()
        resolve()
      }
    } else {
      resolve()
    }
  })
}
/**
* 显示消息提示框
* @param content 提示的标题
*/
export function toast(content) {
  uni.showToast({
    icon: 'none',
    title: content
  })
}

/**
* 显示模态弹窗
* @param content 提示的标题
*/
export function showConfirm(content,config = undefined) {
  return new Promise((resolve, reject) => {
    const mConfig = Object.assign({
      title: i18n.t('common.reminder'),
      content: content,
      cancelText: i18n.t('common.cancel'),
      confirmText: i18n.t('common.sure'),
      success: function(res) {
        resolve(res)
      }
    },config || {})
    uni.showModal(mConfig)
  })
}

/**
* 参数处理
* @param params 参数
*/
export function tansParams(params) {
  let result = ''
  for (const propName of Object.keys(params)) {
    const value = params[propName]
    var part = encodeURIComponent(propName) + "="
    if (value !== null && value !== "" && typeof (value) !== "undefined") {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
            let params = propName + '[' + key + ']'
            var subPart = encodeURIComponent(params) + "="
            result += subPart + encodeURIComponent(value[key]) + "&"
          }
        }
      } else {
        result += part + encodeURIComponent(value) + "&"
      }
    }
  }
  return result
}